﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using AioNightly.Dao;
using AioNightly.Model;
using AioNightly.Common;

namespace AioNightly
{
    public partial class _Default : System.Web.UI.Page
    {
        private string m_selectedProductName;
        private string m_selectedVersion;
        private string m_selectedReleaseDate;

        private string m_defaultStr = "Please Select";

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                CheckUser();

                LoadSession();

                GetProducts();
                StoreDefaultSelect();

                if (m_selectedReleaseDate!=null)
                {
                    RestoreFromSession();
                }
            }
        }

        private User m_user;
        private void CheckUser()
        {
            m_user = (User)Session["User"];

            if (m_user == null)
            {
                Response.Redirect("~/Account/Login.aspx");
            }
        }

        private void Save2Session()
        {
            if (m_selectedProductName != null)
            {
                Session["Product"] = m_selectedProductName;
            }
            if (m_selectedVersion!=null)
            {
                Session["Version"] = m_selectedVersion;
            }
            if (m_selectedReleaseDate!=null)
            {
                Session["Build"] = m_selectedReleaseDate;
            }
        }

        private void RestoreFromSession()
        {
            ddl_product.SelectedValue = m_selectedProductName;

            GetVersionByProductName(m_selectedProductName);
            ddl_version.SelectedValue = m_selectedVersion;

            GetBuildByVersion(m_selectedVersion);
            ddl_time.SelectedValue = m_selectedReleaseDate;
        }

        private void LoadSession()
        {
            m_selectedProductName = (string)Session["Product"];
            m_selectedVersion = (string)Session["Version"];
            m_selectedReleaseDate = (string)Session["Build"];
        }

        private void StoreDefaultSelect()
        {
            if (ddl_product.Items.Count > 0)
            {
                if (ddl_product.Items[0].Value != m_defaultStr)
                {
                    ddl_product.Items.Insert(0, m_defaultStr);
                }
            }
            else
            {
                ddl_product.Items.Insert(0, m_defaultStr);
            }
        }

        private void GetProducts()
        {
            ProductDAO m_productDAO = new ProductDAO();
            Product[] m_productArray = m_productDAO.GetAllProducts().ToArray();
            ddl_product.DataSource = m_productArray;
            ddl_product.DataTextField = "Name";
            ddl_product.DataBind();
            StoreDefaultSelect();
        }

        private void GetVersionByProductName(string productName)
        {
            PVersionDAO m_pversionDAO = new PVersionDAO();
            PVersion[] m_pversionArray = m_pversionDAO.GetVersionByProductName(productName).ToArray();
            ddl_version.DataSource = m_pversionArray;
            ddl_version.DataTextField = "VersionName";
            ddl_version.DataBind();

            if (ddl_version.Items.Count > 0)
            {
                if (ddl_version.Items[0].Value != m_defaultStr)
                {
                    ddl_version.Items.Insert(0, m_defaultStr);
                }
            }
        }

        private void GetBuildByVersion(string versionName)
        {
            BuildDAO m_buildDAO = new BuildDAO();
            Build[] m_buildArray = m_buildDAO.GetBuildByVersion(versionName).ToArray();
            ddl_time.DataSource = m_buildArray;
            ddl_time.DataTextField = "CreateTime";
            ddl_time.DataBind();

            if (ddl_time.Items.Count > 0)
            {
                if (ddl_time.Items[0].Value != m_defaultStr)
                {
                    ddl_time.Items.Insert(0, m_defaultStr);
                }
            }
        }

        private void ClearDropList(DropDownList list)
        {
            list.Items.Clear();
        }

        protected void OnProductChanged(object sender, EventArgs e)
        {
            ClearDropList(ddl_version);
            ClearDropList(ddl_time);

            m_selectedProductName = ddl_product.SelectedItem.Value;
            GetVersionByProductName(m_selectedProductName);

            Save2Session();
        }

        protected void OnVersionChanged(object sender, EventArgs e)
        {
            ClearDropList(ddl_time);

            m_selectedVersion = ddl_version.SelectedItem.Value;
            GetBuildByVersion(m_selectedVersion);

            Save2Session();
        }

        protected void OnBuildChanged(object sender, EventArgs e)
        {
            ItemDAO m_itemDAO = new ItemDAO();

            Item[] m_itemSummaryArray = m_itemDAO.GetItemByRelease((int)TypeManager.ItemType.Summary, ddl_time.SelectedItem.Value).ToArray();
            rp_summary.DataSource = m_itemSummaryArray;
            rp_summary.DataBind();

            Item[] m_itemBugArray = m_itemDAO.GetItemByRelease((int)TypeManager.ItemType.Bug, ddl_time.SelectedItem.Value).ToArray();
            rp_bug.DataSource = m_itemBugArray;
            rp_bug.DataBind();

            Save2Session();
        }

        protected void rp_summary_ItemDataBound(object sender, RepeaterItemEventArgs e)
        {
            if (e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem)
            {
                ((HyperLink)e.Item.FindControl("hl_summary")).Text = string.Format("{0}. {1}", e.Item.ItemIndex + 1, ((Item)e.Item.DataItem).Content);
                ((HyperLink)e.Item.FindControl("hl_summary")).NavigateUrl = string.Format("~/Note.aspx?note={0}", ((Item)e.Item.DataItem).Id);

                switch (((Item)e.Item.DataItem).State)
                {
                    case (int)TypeManager.ItemState.Rejected:
                        ((HyperLink)e.Item.FindControl("hl_summary")).ForeColor = System.Drawing.Color.Red;
                        break;

                    case (int)TypeManager.ItemState.Verified:
                        ((HyperLink)e.Item.FindControl("hl_summary")).ForeColor = System.Drawing.Color.Green;
                        break;

                    default:
                        ((HyperLink)e.Item.FindControl("hl_summary")).ForeColor = System.Drawing.Color.Black;
                        break;
                }
            }
        }

        protected void rp_bug_ItemDataBound(object sender, RepeaterItemEventArgs e)
        {
            if (e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem)
            {
                ((HyperLink)e.Item.FindControl("hl_bug")).Text = string.Format("{0}. {1}", e.Item.ItemIndex + 1, ((Item)e.Item.DataItem).Content);
                ((HyperLink)e.Item.FindControl("hl_bug")).NavigateUrl = string.Format("~/Note.aspx?note={0}", ((Item)e.Item.DataItem).Id);

                switch (((Item)e.Item.DataItem).State)
                {
                    case (int)TypeManager.ItemState.Rejected:
                        ((HyperLink)e.Item.FindControl("hl_bug")).ForeColor = System.Drawing.Color.Red;
                        break;

                    case (int)TypeManager.ItemState.Verified:
                        ((HyperLink)e.Item.FindControl("hl_bug")).ForeColor = System.Drawing.Color.Green;
                        break;

                    default:
                        ((HyperLink)e.Item.FindControl("hl_bug")).ForeColor = System.Drawing.Color.Black;
                        break;
                }
            }
        }
    }
}
